﻿using System;
using System.Collections.Generic;
using System.Linq;
using T.FAS.Runtime.Action.Common;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Expression.Entity;
using T.FAS.Runtime.Metadata.SPI;
using System.Text;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.Action.ExpressionPlugin
{
    public class DataQueryDetailExpProvider : BaseProvider, IExprProvider
    {
        public List<ExprItem> GetExprChildrenItems(ExprItem item, bool IsContainExt, string projectBranchId)
        {
            if (item == null)
                throw new ArgumentNullException("DataQueryDetailExpProvider.GetExprChildrenItems.item");
            UpdateExprItemChildren(item, IsContainExt, projectBranchId);
            return item.Children;
        }

        public List<ExprItem> GetExprItems(string type, string typeId, string exprMetaDataID)
        {
            return new List<ExprItem>();
        }

        public List<ExprItem> GetExprChildrenItems(List<ExprItem> item, bool IsContainExt, string projectBranchId)
        {
            if (item == null || item.Count <= 0)
                return null;
            //foreach(var internalItem in item)
            //{
            //    if (internalItem.IsDelete)
            //        continue;
            //    try
            //    {
            //        BuildExprItemOneByOne(IsContainExt, internalItem);
            //    }
            //    catch (Exception ex)
            //    {
            //        internalItem.ItemType = ItemType.Error;
            //        internalItem.DisplayName = ex.Message;
            //    }
            //}
            GetExprChildrenItemsParallel(item, IsContainExt, projectBranchId);
            return item;
        }
        private void GetExprChildrenItemsParallel(List<ExprItem> items, bool IsContainExt, string projectBranchId)
        {
            int parallelCount = 100;
            List<List<ExprItem>> groupItems = new List<List<ExprItem>>();
            if (items.Count > parallelCount)
            {
                double groupCount = Math.Ceiling((double)items.Count / parallelCount);
                for (int i = 0; i < groupCount; i++)
                {
                    var subList = items.Skip(i * parallelCount).Take(parallelCount).ToList();
                    groupItems.Add(subList);
                }
                groupItems.AsParallel().ForAll(t =>
                {
                    BatchGetExprChildrenItems(t, IsContainExt, projectBranchId);
                });
            }
            else
            {
                BatchGetExprChildrenItems(items, IsContainExt, projectBranchId);
            }
        }
        private void BatchGetExprChildrenItems(List<ExprItem> items, bool IsContainExt, string projectBranchId)
        {
            foreach (var i in items)
            {
                if (i.IsDelete)
                    continue;
                try
                {
                    BuildExprItemOneByOne(IsContainExt, i, projectBranchId);
                }
                catch (Exception ex)
                {
                    i.ItemType = ItemType.Error;
                    i.DisplayName = ex.Message;
                }
            }
        }


        /// <summary>
        /// 构造与表达式结构一致的TResult结构，提供给前端绑定用
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public TRecord BuildTResultSchema(ExprItem item)
        {
            var result = new TRecord();
            var schema = UpdateExprItemChildren(item);
            var propertiesDic = new Dictionary<string, object>();
            propertiesDic.Add("ActionID", schema.ActionID);
            propertiesDic.Add("ActionCode", schema.ActionCode);
            propertiesDic.Add("ActionName", schema.ActionName);
            if (item.Children != null && item.Children.Count > 0)
            {
                item.Children.ForEach(expr =>
                {
                    if (expr.IsLeaf)
                    {
                        propertiesDic[expr.Code] = "";
                    }
                    else
                    {
                        propertiesDic[expr.Code] = BuildRecordSchemaByExprItem(expr.Children);
                    }
                });
            }
            result = new TRecord(propertiesDic);
            return result;
        }
        public List<ExprItem> GetExprIncrementalItems(string exprMetaDataID, DateTime dateTime)
        {
            return new List<ExprItem>();
        }


        #region 内部方法

        private void BuildExprItemOneByOne(bool IsContainExt, ExprItem internalItem, string projectBranchID = "")
        {
            try
            {
                string relatedCdoCode = "";
                var DataQuery = GetDataQueryByID(internalItem.ID, projectBranchID,out relatedCdoCode);
                if (DataQuery != null)
                {
                    AppendCdoInfo(internalItem,relatedCdoCode);
                    TSchema schema = DataQuery.BuildSchema();
                    schema.ActionID = DataQuery.ID;
                    schema.ActionCode = DataQuery.Code;
                    schema.ActionName = DataQuery.Name;
                    UpdateExprItem(internalItem, DataQuery, schema, IsContainExt);
                    UpdateHelpInfo(internalItem, DataQuery);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"获取DQ:{internalItem.ID}元数据失败, msg:{e.Message}\r\ne:{e}");
            }
        }

        private TRecord BuildRecordSchemaByExprItem(List<ExprItem> items)
        {
            var propertiesDic = new Dictionary<string, object>();
            foreach (var item in items)
            {
                if (item.IsLeaf)
                {
                    propertiesDic[item.Code] = "";
                }
                else
                {
                    if (item.Children != null)
                    {
                        var subPropertiesDic = new Dictionary<string, object>();
                        foreach (var expr in item.Children)
                        {
                            if (expr.IsLeaf)
                            {
                                //subPropertiesDic.Add(expr.Code, "");
                                subPropertiesDic[expr.Code] = "";
                            }
                            else
                            {
                                var childRecord = BuildRecordSchemaByExprItem(expr.Children);
                                //subPropertiesDic.Add(expr.Code, childRecord);
                                subPropertiesDic[expr.Code] = childRecord;
                            }
                        }
                        var subRecord = new TRecord(subPropertiesDic);
                        //propertiesDic.Add(item.Code,subRecord);
                        propertiesDic[item.Code] = subRecord;
                    }
                    else
                    {
                        //propertiesDic.Add(item.Code,"");
                        propertiesDic[item.Code] = "";
                    }
                }
            }
            return new TRecord(propertiesDic);
        }



        private TSchema UpdateExprItemChildren(ExprItem item, bool IsContainExt = false, string projectBranchID = "")
        {
            var schema = new TSchema();
            var relatedCdoCode = "";
            //var dq = new T.FAS.Runtime.DataModel.Plugin.DataQueryManager().GetMetaDataContent(item.ID) as DataQuery;
            var dq = GetDataQueryByID(item.ID, projectBranchID, out relatedCdoCode);
            if (dq != null)
            {
                AppendCdoInfo(item,relatedCdoCode);
                schema = dq.BuildSchema();
                schema.ActionID = dq.ID;
                schema.ActionCode = dq.Code;
                schema.ActionName = dq.Name;
                schema.ResultType = DataType.List;
                UpdateExprItem(item, dq, schema, IsContainExt);
            }
            return schema;
        }

        /// <summary>
        /// 获取DQ元数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private DataQuery GetDataQueryByID(string id, string projectBranchID,out string relatedCdoCode)
        {
            relatedCdoCode = "";
            var dataQuery = base.GetMetaDataContentByType(id, ProviderMetadataType.DQ, projectBranchID) as DataQuery;
            if (dataQuery != null)
            {
                if(base.IsDev)
                    relatedCdoCode = GetCDOCodeByDQID(id,projectBranchID);
            }
            return dataQuery;
        }

        /// <summary>
        /// 构造表达式树-构造DQ顶级节点
        /// </summary>
        /// <param name="exprItem"></param>
        /// <param name="DataQuery"></param>
        /// <param name="schema"></param>
        /// <param name="IsContainExt"></param>
        private void UpdateExprItem(ExprItem exprItem, DataQuery DataQuery, TSchema schema, bool IsContainExt = false)
        {
            if (schema == null)
                return;
            IRefSchemaEntity refSchemaEntity = RefSchemaExprUtil.GetRefSchemaEntity(DataQuery);
            DataType dType = DataType.List; //DQ返回值默认为List
            exprItem.Code = DataQuery.Code;
            exprItem.Name = DataQuery.Name;
            exprItem.IsLeaf = false;
            exprItem.HelpInfo = DataQuery.Name;
            exprItem.ItemType = ItemType.DataQuery;
            exprItem.ValueType = dType;
            //填充DataModels
            if (schema.MetadataType == TEntityType.DQ)
            {
                if (schema.Entity != null && schema.Entity.Attributes != null && schema.Entity.Attributes.Count > 0)
                {
                    var subEntities = schema.Entity.Attributes.FindAll(x => x.DataType == DataType.List || x.DataType == DataType.Record);
                    if (subEntities != null && subEntities.Count > 0)
                    {
                        exprItem.DataModels = string.Join(",", subEntities.Select(x => x.MetadataCode));
                    }
                }
            }
            else
            {
                if (schema.Entity != null)
                {
                    exprItem.DataModels = schema.Entity.MetadataCode;
                }
            }

            //构造入参集合
            var inParams = DataQuery.DataQueryParams.FindAll(x => x.IsInputParam);
            inParams.ForEach(p =>
            {
                int index = 0;
                var itemParam = new ExprFuncItemParam()
                {
                    ID = Guid.NewGuid().ToString(),
                    ExprItemID = exprItem.ID,
                    Code = p.Code,
                    Name = p.Name,
                    SortOrder = index,
                    DataType = (DataType)Enum.Parse(typeof(DataType), p.ParamType, true),
                };
                if (IsContainExt)
                {
                    var extDic = new Dictionary<string, object>();
                    extDic.Add(ExpressionPluginManager.ExprExt_IsSysDefault, p.IsDefault);
                    itemParam.ExprExt = extDic;
                }
                exprItem.ExprFuncItemParamList.Add(itemParam);
                index++;
            });

            exprItem.DisplayName = "DataQuery." + DataQuery.Code + $"({string.Join(",", inParams.Select(p => p.Code))})";
            string refExpr = DataQuery.GetTopRefSchemaExpr();
            exprItem.RefSchemaExpr = RefSchemaExprUtil.GetExpr(refExpr);
            exprItem.RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, refExpr, exprItem.IsLeaf);
            if (dType == DataType.List)
            {
                if (schema.Entity.ListTypeMethods != null)
                {
                    //增加List节点
                    List<TListMethod> methods = schema.Entity.ListTypeMethods.OrderBy(x => x.SortOrder).ToList();
                    foreach (var method in methods)
                    {
                        BuildListTypeMethodExprItem(exprItem, method, schema.Entity, true, IsContainExt, metaDataContent: DataQuery);
                    }
                }
            }
            else if (dType == DataType.Record)
            {
                //增加Record节点
                var recordNode = new ExprItem()
                {
                    ID = "Record",
                    Code = "Record",
                    Name = "Record",
                    DisplayName = exprItem.DisplayName + ".Record",
                    RefSchemaExpr = RefSchemaExprUtil.GetExpr(exprItem.RefSchemaExpr),
                    RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, exprItem.RefSchemaExpr, false),
                    ItemType = ItemType.Object,
                    ValueType = DataType.Record,
                    HelpInfo = DataQuery.Code + "Record",
                    SortOrder = 0,
                    ExprMetaDataID = exprItem.ExprMetaDataID,
                    ParentID = exprItem.ID,
                    IsLeaf = false,
                    DataModels = schema.Entity.MetadataCode
                };
                recordNode.DataModels = schema.Entity.MetadataCode;
                BuildTEntityExprItem(recordNode, schema.Entity, IsContainExt, DataQuery);
                exprItem.Children.Add(recordNode);
            }
        }

        /// <summary>
        /// 根据XXX.List.{sub Methods}的方法集合继续扩展下级节点
        /// 例如GetXXXBasicList.List.{Current|Count|Rows|CurrentRowNumber)等
        /// </summary>
        /// <param name="parentItem"></param>
        /// <param name="method"></param>
        /// <param name="entity"></param>
        /// <param name="isRoot"></param>
        /// <param name="IsContainExt"></param>
        /// <param name="metaDataContent"></param>
        private void BuildListTypeMethodExprItem(ExprItem parentItem, TListMethod method, TEntity entity, bool isRoot = false, bool IsContainExt = false, IMetaDataContent metaDataContent = null)
        {
            var item = new ExprItem();

            IRefSchemaEntity refSchemaEntity = RefSchemaExprUtil.GetRefSchemaEntity(metaDataContent);
            //当方法名为List时
            if (!isRoot && method.Code.ToLower().Equals("list"))
            {
                bool isLeafFlag = (method.SubMethods == null || method.SubMethods.Count <= 0) ? true : false;
                item = new ExprItem()
                {
                    ID = entity.MetadataID,
                    Code = entity.MetadataCode,
                    Name = entity.MetadataName,
                    DisplayName = parentItem.DisplayName + "." + entity.MetadataCode,
                    RefSchemaExpr = RefSchemaExprUtil.GetExpr(parentItem.RefSchemaExpr, false),
                    RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, parentItem.RefSchemaExpr, isLeafFlag),
                    ValueType = method.ReturnDataType,
                    ItemType = ItemType.Object,
                    SortOrder = method.SortOrder,
                    ExprMetaDataID = parentItem.ExprMetaDataID,
                    ParentID = parentItem.ID,
                    IsLeaf = isLeafFlag,
                    DataModels = entity.MetadataCode,
                };
            }
            else
            {
                bool isLeafFlag = (method.SubMethods == null || method.SubMethods.Count <= 0) ? true : false;
                item = new ExprItem()
                {
                    ID = Guid.NewGuid().ToString(),
                    Code = method.Code,
                    Name = method.Code,
                    DisplayName = parentItem.DisplayName + "." + method.Code,
                    RefSchemaExpr = RefSchemaExprUtil.GetExpr(parentItem.RefSchemaExpr, false),
                    RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, parentItem.RefSchemaExpr, isLeafFlag),
                    ValueType = method.ReturnDataType,
                    ItemType = ItemType.Object,
                    HelpInfo = parentItem.DisplayName + "." + method.Code,
                    SortOrder = method.SortOrder,
                    ExprMetaDataID = parentItem.ExprMetaDataID,
                    ParentID = parentItem.ID,
                    IsLeaf = isLeafFlag,
                    ExprExt = parentItem.ExprExt,
                };
                if (entity.Attributes != null && entity.Attributes.Count > 0)
                {
                    item.DataModels = string.Join(",", entity.MetadataCode);
                }
            }
            //当方法名为Current时
            if (!string.IsNullOrEmpty(method.JSFunc) && method.JSFunc.ToLower().Equals(("@entity" + entity.MetadataCode).ToLower()))
            {
                item.IsLeaf = false;
                //构造Current下级的Entity结构
                BuildTEntityExprItem(parentItem: item, entity, IsContainExt, metaDataContent);
            }
            else
            {
                if (method.SubMethods != null && method.SubMethods.Count > 0)
                {
                    foreach (var subMethod in method.SubMethods)
                    {
                        BuildListTypeMethodExprItem(parentItem: item, subMethod, entity, IsContainExt: IsContainExt, metaDataContent: metaDataContent);
                    }
                }
            }
            parentItem.Children.Add(item);
        }

        /// <summary>
        /// 根据Entity结构构造ExprItems
        /// </summary>
        /// <param name="parentItem"></param>
        /// <param name="entity"></param>
        /// <param name="IsContainExt"></param>
        /// <param name="metaDataContent"></param>
        private void BuildTEntityExprItem(ExprItem parentItem, TEntity entity, bool IsContainExt = false, IMetaDataContent metaDataContent = null)
        {
            if (entity == null)
                return;

            IRefSchemaEntity refSchemaEntity = RefSchemaExprUtil.GetRefSchemaEntity(metaDataContent);
            //针对DQ有多个DO的情况，需要单独处理
            if (entity.MetadataType == TEntityType.DQ)
            {
                //处理一个DQ下有多个DO,
                var subEntitys = entity.Attributes.FindAll(x => x.DataType == DataType.List || x.DataType == DataType.Record);
                if (subEntitys != null && subEntitys.Count > 1)
                {
                    var itemIndex = 0;
                    foreach (TAttribute item in subEntitys)
                    {
                        var itemNode = new ExprItem()
                        {
                            ID = item.Entity.MetadataID,
                            //Code = item.Entity.MetadataCode,
                            Code = item.Code,
                            Name = item.Entity.MetadataName,
                            DisplayName = parentItem.DisplayName + "." + item.Entity.MetadataCode,
                            RefSchemaExpr = RefSchemaExprUtil.GetExpr(item.RefSchemaExpr),
                            RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, item.RefSchemaExpr, false),
                            DataModels = item.Entity.MetadataCode,
                            HelpInfo = item.Entity.MetadataCode,
                            SortOrder = itemIndex,
                            ValueType = item.Entity.ResultType,
                            ExprMetaDataID = parentItem.ExprMetaDataID,
                            ParentID = parentItem.ID,
                            IsLeaf = false,
                            ItemType = GetItemTypeByTEntityType(item.Entity.MetadataType)
                        };
                        if (item.Entity.Attributes != null && item.Entity.Attributes.Count > 0)
                        {
                            BuildPropertyExprItem(itemNode, item.Entity, IsContainExt, metaDataContent: metaDataContent);
                        }
                        parentItem.Children.Add(itemNode);
                        itemIndex++;
                    }
                }
                //处理一个DQ下只有一个DO
                else if (subEntitys != null && subEntitys.Count == 1)
                {
                    if (subEntitys[0].Entity.Attributes != null && subEntitys[0].Entity.Attributes.Count > 0)
                    {
                        BuildTEntityExprItem(parentItem, subEntitys[0].Entity, IsContainExt, metaDataContent: metaDataContent);
                    }
                }
                //处理Attribute
                List<TAttribute> attributes = entity.Attributes.FindAll(x => x.DataType != DataType.List && x.DataType != DataType.Record);
                if (attributes != null && attributes.Count > 0)
                {
                    var attrIndex = 0;
                    foreach (TAttribute attr in attributes)
                    {
                        //跳过辅助语言字段
                        if (attr.IsNeedHideField(base.IsDev)) continue;
                        var itemNode = new ExprItem()
                        {
                            ID = attr.ID,
                            Code = attr.Code,
                            Name = attr.Name,
                            DisplayName = parentItem.DisplayName + "." + attr.Code,
                            RefSchemaExpr = RefSchemaExprUtil.GetExpr(attr.RefSchemaExpr),
                            RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, attr.RefSchemaExpr, true),
                            HelpInfo = attr.Code,
                            SortOrder = attrIndex,
                            ValueType = attr.DataType,
                            ExprMetaDataID = parentItem.ExprMetaDataID,
                            ParentID = parentItem.ID,
                            IsLeaf = true,
                            ItemType = GetItemTypeByTEntityType(TEntityType.Attribute)
                        };
                        //是否需要包含扩展属性
                        if (IsContainExt)
                        {
                            var extDic = new Dictionary<string, object>();
                            extDic.Add(ExpressionPluginManager.ExprExt_Length, attr.Length);
                            extDic.Add(ExpressionPluginManager.ExprExt_IsRequired, attr.IsRequired);
                            extDic.Add(ExpressionPluginManager.ExprExt_Precision, attr.Precision);
                            extDic.Add(ExpressionPluginManager.ExprExt_DefaultValue, attr.DefaultValue);

                            if (attr.ExtProperties != null && attr.ExtProperties.ContainsKey(nameof(DataObjectColumnHelpSetting)))
                            {
                                extDic[nameof(DataObjectColumnHelpSetting)] = attr.ExtProperties[nameof(DataObjectColumnHelpSetting)];
                            }
                            itemNode.ExprExt = extDic;
                        }
                        parentItem.Children.Add(itemNode);
                        attrIndex++;
                    }
                }
            }
            //针对非DQ的,主节点不需要体现,直接体现属性字段
            else
            {
                if (entity.Attributes != null && entity.Attributes.Count > 0)
                {
                    BuildPropertyExprItem(parentItem, entity, IsContainExt, metaDataContent: metaDataContent);
                }
            }
        }

        private ItemType GetItemTypeByTEntityType(TEntityType eType)
        {
            switch (eType)
            {
                case TEntityType.DO:
                    return ItemType.DO;
                case TEntityType.CDO:
                    return ItemType.DO;
                case TEntityType.SGEO:
                case TEntityType.EO:
                    return ItemType.EO;
                case TEntityType.Attribute:
                    return ItemType.Attribute;
                default:
                    return ItemType.Object;

            }
        }

        /// <summary>
        /// 构造TEntity属性映射的ExprItems
        /// </summary>
        /// <param name="parentItem"></param>
        /// <param name="entity"></param>
        /// <param name="IsContainExt"></param>
        /// <param name="metaDataContent"></param>
        private void BuildPropertyExprItem(ExprItem parentItem, TEntity entity, bool IsContainExt = false, IMetaDataContent metaDataContent = null)
        {
            if (entity == null || entity.Attributes == null || entity.Attributes.Count <= 0)
                return;

            IRefSchemaEntity refSchemaEntity = RefSchemaExprUtil.GetRefSchemaEntity(metaDataContent);
            var proIndex = 0;
            foreach (TAttribute attr in entity.Attributes.OrderBy(x => x.SortOrder))
            {
                if (attr.DataType == DataType.Record)
                {
                    var pRecordNode = new ExprItem()
                    {
                        ID = attr.Entity.MetadataCode,
                        Code = attr.Entity.MetadataCode,
                        Name = attr.Entity.MetadataName,
                        DisplayName = parentItem.DisplayName + "." + attr.Entity.MetadataCode,
                        RefSchemaExpr = RefSchemaExprUtil.GetExpr(attr.Entity.RefSchemaExpr),
                        RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, attr.Entity.RefSchemaExpr, false),
                        DataModels = attr.Entity.MetadataCode,
                        ItemType = ItemType.Object,
                        HelpInfo = attr.Name,
                        SortOrder = proIndex,
                        IsLeaf = false,
                        ValueType = DataType.Record
                    };
                    BuildPropertyExprItem(pRecordNode, attr.Entity, IsContainExt, metaDataContent);
                    parentItem.Children.Add(pRecordNode);
                }
                else if (attr.DataType == DataType.List)
                {
                    //增加List节点
                    attr.Entity.ListTypeMethods.OrderBy(x => x.SortOrder).ToList().ForEach(method =>
                    {
                        BuildListTypeMethodExprItem(parentItem, method, attr.Entity, IsContainExt: IsContainExt, metaDataContent: metaDataContent);
                    });
                }
                else
                {
                    //跳过辅助语言字段
                    if (attr.IsNeedHideField(base.IsDev)) continue;
                    var pNode = new ExprItem()
                    {
                        ID = attr.ID,
                        Code = attr.Code,
                        Name = attr.Name,
                        DisplayName = parentItem.DisplayName + "." + attr.Code,
                        RefSchemaExpr = RefSchemaExprUtil.GetExpr(attr.RefSchemaExpr),
                        RefSchemaExprValue = RefSchemaExprUtil.GetExprValue(refSchemaEntity, attr.RefSchemaExpr, true),
                        ItemType = ItemType.Attribute,
                        HelpInfo = parentItem.DisplayName + "." + attr.Code,
                        SortOrder = proIndex,
                        ValueType = attr.DataType,
                        ExprMetaDataID = parentItem.ExprMetaDataID,
                        IsLeaf = true,
                    };
                    if (IsContainExt)
                    {
                        var extDic = new Dictionary<string, object>();
                        if (attr.ExtProperties != null && attr.ExtProperties.ContainsKey(nameof(DataObjectColumnHelpSetting)))
                        {
                            extDic[nameof(DataObjectColumnHelpSetting)] = attr.ExtProperties[nameof(DataObjectColumnHelpSetting)];
                        }
                        pNode.ExprExt = extDic;
                    }

                    parentItem.Children.Add(pNode);
                }
                proIndex++;
            }
        }

        private void UpdateHelpInfo(ExprItem item, DataQuery dataQuery)
        {
            switch (item.ItemType)
            {
                case ItemType.DataQuery:
                    {
                        StringBuilder helpInfo = new StringBuilder();
                        helpInfo.Append($"<div ad><div at>数据查询：</div>{item.Code}，{item.Name}</div>"); 
                        helpInfo.Append($"<div ad><div at>返回值类型：</div>{item.ValueType}</div>");
                        //helpInfo.Append($"DataQuery:<b>{item.Code}</b> DataType <b>{item.DataModels}</b><br/>");
                        //helpInfo.Append("<br/>");
                        //helpInfo.Append($"Params:<br/>");

                        if (item.ExprFuncItemParamList!=null && item.ExprFuncItemParamList.Count > 0)
                        {
                            helpInfo.Append($"<div ad><div at>入参：</div>");
                            foreach (var param in item.ExprFuncItemParamList)
                            {
                                // helpInfo.Append($"<b>{p.Code}</b> DataType: <b>{p.DataType}</b><br/>");
                                helpInfo.Append($"{param.Code}, 类型：{param.DataType}<br/>");
                            }
                            helpInfo.Append($"</div>");
                        }                        
                        //helpInfo.Append("<br/>");
                        //helpInfo.Append($"ReturnValueType:<b>{item.ValueType}</b><br/>");
                        item.HelpInfo = helpInfo.ToString();
                    }
                    break;
                case ItemType.Attribute:
                    {
                        StringBuilder helpInfo = new StringBuilder();
                        //helpInfo.Append($"<b>{item.Code}</b> DataType <b>{item.ValueType}</b><br/>");
                        helpInfo.Append($"<div ad><div at>属性：</div>{item.Code}，{item.Name}</div>");
                        helpInfo.Append($"<div ad><div at>类型：</div>{item.ValueType}</div>");
                        item.HelpInfo = helpInfo.ToString();
                    }
                    break;
                case ItemType.DO:
                    {
                        StringBuilder helpInfo = new StringBuilder();
                        helpInfo.Append($"<div ad><div at>数据对象：</div>{item.DataModels}</div>");
                        helpInfo.Append($"<div ad><div at>返回值类型：</div>DO</div>");
                        // helpInfo.Append($"DataType <b>{item.DataModels} DO</b><br/>");
                        item.HelpInfo = helpInfo.ToString();
                    }
                    break;
                case ItemType.EO:
                    {
                        StringBuilder helpInfo = new StringBuilder();
                        helpInfo.Append($"<div ad><div at>实体对象：</div>{item.DataModels}</div>");
                        helpInfo.Append($"<div ad><div at>返回值类型：</div>EO</div>");
                        // helpInfo.Append($"DataType <b>{item.DataModels} EO</b><br/>");
                        item.HelpInfo = helpInfo.ToString();
                    }
                    break;
                case ItemType.Object:
                    {
                        StringBuilder helpInfo = new StringBuilder();

                        if (item.ValueType == DataType.List)
                        {
                            //helpInfo.Append($"DataType <b>{item.DataModels} List</b><br/>");
                            helpInfo.Append($"<div ad><div at>对象：</div>List&lt;{item.DataModels}&gt;</div>");
                        }
                        else if (item.ValueType == DataType.Record)
                        {
                            //helpInfo.Append($"DataType <b>{item.DataModels} Record</b><br/>");
                            helpInfo.Append($"<div ad><div at>对象：</div>Record&lt;{item.DataModels}&gt;</div>");
                        }
                        else
                        {
                            //helpInfo.Append($"DataType <b>{item.ValueType}</b><br/>");
                            helpInfo.Append($"<div ad><div at>对象：</div>{item.DataModels}</div>");
                        }
                        helpInfo.Append($"<div ad><div at>返回值类型：</div>{item.ValueType}</div>");
                        item.HelpInfo = helpInfo.ToString();
                    }
                    break;
            }
            if (item.Children != null)
            {
                item.Children.ForEach(c => UpdateHelpInfo(c, dataQuery));
            }
        }

        private string GetCDOCodeByDQID(string metaDataId, string projectBranchID)
        {
            /* Todo
            var parentMetaDatas = new FASProjectHelpMgr().QueryProjectParentTreeList(new List<string> { metaDataId },projectBranchID);
            if (parentMetaDatas != null && parentMetaDatas.Count > 0)
            {
                var targetMetaData = parentMetaDatas[0];
                if (targetMetaData.ItemID == "CDO")
                {
                    return targetMetaData.ItemName;
                }
            }
            */
            return "";
        }
        private void AppendCdoInfo(ExprItem item, string cdoCode)
        {
            if (!string.IsNullOrEmpty(cdoCode))
            {
                Dictionary<string, object> exprDic = new Dictionary<string, object>();
                if (item.ExprExt != null)
                {
                    exprDic = item.ExprExt as Dictionary<string, object>;
                }
                exprDic["RelatedCdoCode"] = cdoCode;
                item.ExprExt = exprDic;
            }
        }

        #endregion
    }
}
